package com.purong.purong.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.bean.LoginBean;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.util.ZeroStartUnableTextWatch;
import com.purong.purong.widget.LoadingView;
import com.purong.purong.widget.TextChoseDialog;
import com.yao.component.pictureselector.photoview.VersionedGestureDetector;
import com.yao.sdk.glide.GlideUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/11.
 */
public class UserBaseInfoActivity extends BaseActivity {
    private EditText nameEd,identifyEd,ageEd,birthAddEd,domAddEd,workAddEd;
    private TextView nameTv,identifyTv,sexTv;
    private LoadingView loadingView;
    private LoginBean bean;
    private TextChoseDialog mDialog;
    private TextItemBean sexBean;
    @Override
    public int getRootViewId() {
        return R.layout.activity_user_base_info;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("个人基本信息");
        loadingView=(LoadingView)findViewById(R.id.loading_view);
        loadingView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getBaseInfo();
            }

            @Override
            public void onRefresh() {

            }
        });
        nameTv=(TextView)findViewById(R.id.name_tv);
        identifyTv=(TextView)findViewById(R.id.identify_tv);
        nameEd=(EditText)findViewById(R.id.name_ed);
        identifyEd=(EditText)findViewById(R.id.identify_ed);
        sexTv=(TextView)findViewById(R.id.sex_tv);
        ageEd=(EditText)findViewById(R.id.age_ed);
        birthAddEd=(EditText)findViewById(R.id.birth_add_ed);
        domAddEd=(EditText)findViewById(R.id.dom_add_ed);
        workAddEd=(EditText)findViewById(R.id.work_add_ed);
        ageEd.addTextChangedListener(new ZeroStartUnableTextWatch(ageEd));
        nameEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!bean.getIs_real().equals("2")){
                    if (hasFocus){
                        if (nameEd.getText().toString().trim().equals(bean.getReal())){
                            nameEd.setText("");
                        }
                    }else {
                        if (nameEd.getText().toString().trim().equals("")){
                            nameEd.setText(bean.getReal());
                        }
                    }
                }

            }
        });
        identifyEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!bean.getIs_real().equals("2")){
                    if (hasFocus){
                        if (identifyEd.getText().toString().trim().equals(bean.getIdentity())){
                            identifyEd.setText("");
                        }
                    }else {
                        if (identifyEd.getText().toString().trim().equals("")){
                            identifyEd.setText(bean.getIdentity());
                        }
                    }
                }

            }
        });
    }
    private void getBaseInfo(){
        loadingView.loading();
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!request(Contant.LOGIN_INFO, params, true,false)){
            loadingView.fail();
        }
    }
    private void setBaseInfo(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!bean.getIs_real().equals("2")){
            if (nameEd.getText().toString().trim().equals("")){
                showToast("姓名为必填项");
                return;
            }else {
                if (!nameEd.getText().toString().trim().equals(bean.getReal())){
                    params.put("real",nameEd.getText().toString().trim());
                }
            }
            if (identifyEd.getText().toString().trim().equals("")){
                showToast("身份证号为必填项");
                return;
            }else {
                if (!identifyEd.getText().toString().trim().equals(bean.getIdentity())){
                    params.put("identity",identifyEd.getText().toString().trim());
                }
            }

        }
        if (sexBean==null){
            showToast("性别为必选项");
            return;
        }else {
            params.put("sex",sexBean.getId());
        }
        if (ageEd.getText().toString().trim().equals("")){
            showToast("年龄为必填项");
            return;
        }else {
            params.put("age",ageEd.getText().toString().trim());
        }
        params.put("birth_address",birthAddEd.getText().toString().trim());
        params.put("domicile_address",domAddEd.getText().toString().trim());
        params.put("work_address",workAddEd.getText().toString().trim());
        request(Contant.SET_INFO, params, true,true);
    }
    private void setData(){
        if (bean==null)
            return;
        if (bean.getIs_real().equals("2")){
            nameTv.setVisibility(View.VISIBLE);
            identifyTv.setVisibility(View.VISIBLE);
            nameTv.setText(bean.getReal());
            identifyTv.setText(bean.getIdentity());
        }else {
            nameEd.setVisibility(View.VISIBLE);
            identifyEd.setVisibility(View.VISIBLE);
            if (bean.getReal()!=null&&!bean.getReal().equals("")){
                nameEd.setText(bean.getReal());
            }
            if (bean.getIdentity()!=null&&!bean.getIdentity().equals("")){
                identifyEd.setText(bean.getIdentity());
            }
        }
        if (bean.getSex()!=null){
            switch (bean.getSex()){
                case "1":
                    sexTv.setText("保密");
                    sexTv.setTextColor(getResources().getColor(R.color.blue));
                    sexBean=new TextItemBean();
                    sexBean.setId("1");
                    sexBean.setName("保密");
                    break;
                case "2":
                    sexTv.setText("男");
                    sexTv.setTextColor(getResources().getColor(R.color.blue));
                    sexBean=new TextItemBean();
                    sexBean.setId("2");
                    sexBean.setName("男");
                    break;
                case "3":
                    sexTv.setText("女");
                    sexTv.setTextColor(getResources().getColor(R.color.blue));
                    sexBean=new TextItemBean();
                    sexBean.setId("3");
                    sexBean.setName("女");
                    break;
            }
        }

        if (bean.getAge()!=null&&!bean.getAge().equals("")){
            ageEd.setText(bean.getAge());
        }
        if (bean.getBirth_address()!=null&&!bean.getBirth_address().equals("")){
            birthAddEd.setText(bean.getBirth_address());
        }
        if (bean.getDomicile_address()!=null&&!bean.getDomicile_address().equals("")){
            domAddEd.setText(bean.getDomicile_address());
        }
        if (bean.getWork_address()!=null&&!bean.getWork_address().equals("")){
            workAddEd.setText(bean.getWork_address());
        }
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOGIN_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    bean= JSON.parseObject(responseInfo.content,LoginBean.class);
                    if (bean!=null){

                        setData();
                        loadingView.finish();
                    }

                }catch (Exception e){
                    loadingView.fail();
                }
            }else {
                loadingView.fail();
            }
        }else if (responseInfo.url.equals(Contant.SET_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    LoginBean bean= JSON.parseObject(responseInfo.content,LoginBean.class);
                    bean.setPwd(PuRongApp.getLoginInfo(this).getPwd());
                    bean.save(this);
                    UserBaseInfoActivity.this.finish();
                }catch (Exception e){

                }
            }
        }
    }
    private void initDialog(){
        mDialog=new TextChoseDialog(this);
        mDialog.setOnTextChoseListener(new TextChoseDialog.OnTextChoseListener() {
            @Override
            public void onChose(int position, TextItemBean item) {
                sexBean=item;
                sexTv.setText(sexBean.getName());
                sexTv.setTextColor(getResources().getColor(R.color.blue));
            }

        });
        List<TextItemBean> itemBeanList=new ArrayList<>();
        TextItemBean itemBean=new TextItemBean();
        itemBean.setId("1");
        itemBean.setName("保密");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("2");
        itemBean.setName("男");
        itemBeanList.add(itemBean);
        itemBean=new TextItemBean();
        itemBean.setId("3");
        itemBean.setName("女");
        itemBeanList.add(itemBean);
        mDialog.setData(itemBeanList);
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.sex_lay:
                if (mDialog==null)
                    initDialog();
                mDialog.show();
                break;
            case R.id.confirm:
                setBaseInfo();
                break;
        }
    }
    @Override
    public void initData() {
        getBaseInfo();
    }

}
